package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringPool;
import com.ccp.dev.qbdms.dao.WQbdFlightJcjlcfDao;
import com.ccp.dev.qbdms.model.WQbdFlightJc;
import com.ccp.dev.qbdms.model.WQbdFlightJcjlcf;
import com.ccp.dev.qbdms.model.WQbdFlightJczb;
import com.ccp.dev.qbdms.model.vo.WQbdFlightJcjlcfParamVo;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.system.dao.SysOrgDao;
import com.ccp.dev.system.model.SysOrg;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ClassDescribe:
 *
 * @author :milihao
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdFlightJcjlcfService extends BaseService<WQbdFlightJcjlcf> {

	@Resource
	private WQbdFlightJcjlcfDao wQbdFlightJcjlcfDao;
	@Resource
	private SysOrgDao sysOrgDao;

	public List<WQbdFlightJcjlcf> getListByRefId(String id) {
		String[] idAndIsReward = id.split(",");
		List<WQbdFlightJcjlcf> wQbdFlightJcjlcfList = wQbdFlightJcjlcfDao
				.getListByRefId(idAndIsReward[0], idAndIsReward[1]);
		if (wQbdFlightJcjlcfList.size() == 0) {
			wQbdFlightJcjlcfList = Lists.newArrayList();
			WQbdFlightJcjlcf wQbdFlightJcjlcf = new WQbdFlightJcjlcf();
			wQbdFlightJcjlcf.setSwmc("型号两总");
			wQbdFlightJcjlcfList.add(wQbdFlightJcjlcf);
			wQbdFlightJcjlcf = new WQbdFlightJcjlcf();
			wQbdFlightJcjlcf.setSwmc("WQbdFlightJcjlcf");
			wQbdFlightJcjlcfList.add(wQbdFlightJcjlcf);
			wQbdFlightJcjlcf = new WQbdFlightJcjlcf();
			wQbdFlightJcjlcfList.add(wQbdFlightJcjlcf);
		}
		return wQbdFlightJcjlcfList;
	}

	/**
	 * 根据组织id查找其所有子组织
	 *
	 * @param orgId 父id
	 * @return 所有子租户和组织
	 */
	public List<SysOrg> querySubSysOrgBySupOrgId(String orgId) {
		SysOrg org = sysOrgDao.getById(orgId);
		Map<String, String> map = new HashMap<>(2);
		map.put("orgId", orgId);
		map.put("pathLike", org.getPath() + StringPool.PERCENT);
		List<SysOrg> childSysOrgList = sysOrgDao.querySubOrgByPath(map);
		return childSysOrgList;
	}

	public int save(WQbdFlightJcjlcfParamVo wQbdFlightJcjlcfParamVo) {
		wQbdFlightJcjlcfDao
				.delByRefId(wQbdFlightJcjlcfParamVo.getRefid(),
						wQbdFlightJcjlcfParamVo.getSfjl().toString());
		if (BeanUtils.isNotEmpty(wQbdFlightJcjlcfParamVo.getwQbdFlightJcjlcfList())) {
			if (wQbdFlightJcjlcfParamVo.getwQbdFlightJcjlcfList().size() > 0) {
				List<WQbdFlightJcjlcf> qbdFlightJcjlcfList = wQbdFlightJcjlcfParamVo
						.getwQbdFlightJcjlcfList();
				for (WQbdFlightJcjlcf wQbdFlightJcjlcf : qbdFlightJcjlcfList) {
					wQbdFlightJcjlcf.setId(ProjectUUIDUtils.getUUIDFor32());
					wQbdFlightJcjlcf.setSfjl(wQbdFlightJcjlcfParamVo.getSfjl());
					wQbdFlightJcjlcf.setRefid(wQbdFlightJcjlcfParamVo.getRefid());
					wQbdFlightJcjlcfDao.add(wQbdFlightJcjlcf);
				}
			}
		}

		return 1;
	}
}
